﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Linq;

namespace WebChatApi.Common.Audit
{
    /// <summary>
    /// Class for Changeset. Each changeset refers to one action, in which there may be several entity related <seealso cref="AuditItem"/> changes.
    /// </summary>
    [DataContract(Name = "Changeset", IsReference = true)]
    public class Changeset
    {
        #region Properties

        /// <summary>
        /// Gets or sets the changeset id.
        /// </summary>
        /// <value>
        /// The changeset id.
        /// </value>
        [DataMember]
        public long ChangesetId
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the audit items.
        /// </summary>
        /// <value>
        /// The audit items.
        /// </value>
        [DataMember]
        public List<AuditItem> AuditItems
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the operator key.
        /// </summary>
        /// <value>
        /// The operator key.
        /// </value>
        [DataMember]
        public Guid OperatorKey
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        /// <value>
        /// The comments.
        /// </value>
        [DataMember]
        public string Comments
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the created stamp.
        /// </summary>
        /// <value>
        /// The created stamp.
        /// </value>
        [DataMember]
        public DateTime CreatedStamp
        {
            get;
            set;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="Changeset"/> class.
        /// </summary>
        public Changeset()
        {
            this.AuditItems = new List<AuditItem>();
        }

        #endregion

        /// <summary>
        /// To the XML.
        /// </summary>
        /// <param name="operatorKey">The operator key.</param>
        /// <returns></returns>
        public XElement ToXml(Guid? operatorKey = null)
        {
            XElement result = XElement.Parse("<Audits></Audits>");

            result.SetAttributeValue("OperatorKey", operatorKey == null ? this.OperatorKey : operatorKey);

            if (AuditItems != null && AuditItems.Count > 0)
            {
                foreach (var one in AuditItems)
                {
                    result.Add(one.ToXml());
                }
            }

            return result;
        }

        /// <summary>
        /// To the SQL parameters.
        /// </summary>
        /// <returns></returns>
        public List<SqlParameter> ToSqlParameters()
        {
            List<SqlParameter> parameters = new List<SqlParameter>();

            parameters.Add(new SqlParameter("@DataXml", this.ToXml().ToString()));
            parameters.Add(new SqlParameter("@Comments", this.Comments));
            parameters.Add(new SqlParameter("@OperatorKey", this.OperatorKey));

            return parameters;
        }

    }
}
